package com.block.channel.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.channel.factory.MonitorChannelFactory;
import com.block.channel.model.dto.MonitorInfoDTO;
import com.block.channel.model.result.CallbackResult;
import com.block.common.service.IChannelCommonService;
import com.block.db.entity.ChannelActionMonitorRecord;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.mapper.ChannelActionMonitorRecordMapper;
import com.block.channel.service.IChannelActionMonitorRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 渠道行为检测报告 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@Service
public class ChannelActionMonitorRecordServiceImpl extends ServiceImpl<ChannelActionMonitorRecordMapper, ChannelActionMonitorRecord> implements IChannelActionMonitorRecordService {

    @Autowired
    IChannelCommonService channelCommonService;

    @Override
    public void saveMonitorInfo(String platform, String channelCode, String actionType, Map<String, String[]> parameterMap, String paramContent){

        ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(channelCode);

        MonitorInfoDTO monitorInfoDTO = MonitorChannelFactory.getService(platform)
                .handleRequestParam(channelBaseInfo, actionType, parameterMap, paramContent);
        if (ObjectUtil.isEmpty(monitorInfoDTO)){
            return;
        }

        ChannelActionMonitorRecord monitorRecord = new ChannelActionMonitorRecord();
        monitorRecord.setPlatform(monitorInfoDTO.getPlatform());
        monitorRecord.setChannelCode(monitorInfoDTO.getChannelCode());
        monitorRecord.setChannelName(monitorInfoDTO.getChannelName());
        monitorRecord.setActionType(monitorInfoDTO.getActionType());
        monitorRecord.setKeyType(monitorInfoDTO.getKeyType());
        monitorRecord.setUniqueKey(monitorInfoDTO.getUniqueKey());
        monitorRecord.setCallbackInfo(monitorInfoDTO.getCallbackInfo());
        monitorRecord.setExtInfo(monitorInfoDTO.getExtInfo());
        monitorRecord.setExtInfo1(monitorInfoDTO.getExtInfo1());
        monitorRecord.setExtInfo2(monitorInfoDTO.getExtInfo2());
        monitorRecord.setMonitorInfo(monitorInfoDTO.getMonitorInfo());

        this.save(monitorRecord);
    }

    @Override
    public void handleCallbackInfo(String platform, String channelCode, String function, Map<String, String[]> parameterMap, String paramContent) {

        ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(channelCode);

        CallbackResult callbackResult = MonitorChannelFactory.getCallbackService(platform).handleCallback(channelBaseInfo, function, parameterMap, paramContent);

    }

}
